<?php
class Encrypt {
	private $device_imei,$device_secret;
	public $temp_str;	

	function __construct($device_imei,$device_secret){
		$this->device_imei = $device_imei;
		$this->device_secret = $device_secret;
	}
	
	/*
	 * makeToken
	 * 生成动态令牌
	 * @param string $device_imei 设备imei
	 * @param string $device_secret 设备私有密钥
	 * @return string
	 */
	public function makeToken(){
		return md5($this->device_imei . $this->device_secret . rand() . time() . COMMUNICATION_KEY);
	}
	
	/*
	 * dataEncrypt
	 * 对数据进行拼接,连接imei,device_secret,通信秘钥(config.php),
	 * 主机名称(config.php),字符串化后的$data,token,当前日期，然后对拼接后的数据进行md5加密
	 * @param string $data
	 * @param string $token
	 * return string
	 */
	 
	 
	//防止无token状态,为此设置$token=''
	public function dataEncrypt($data,$token=''){
		//strval函数可将数组及类之外的变量类型转换成字符串类型
		$str = $this->device_imei . $this->device_secret . COMMUNICATION_KEY . SERVER_HOST_NAME . strval($data) . $token . date('Y-m-d');
		$this->temp_str = $str;
		return md5($str);
	}
}
